package com.lagou.dao.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 课程内容接口的实现类
 */
public class CourseContentDaoImpl implements CourseContentDao {
    QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(Integer id) {
        try {

            String sql = " select id,course_id,section_name,description,order_num,status\n" +
                    "from course_section\n" +
                    "where course_id = ?";
            List<Course_Section> query = qr.query(sql, new BeanListHandler<Course_Section>(Course_Section.class), id);
            for (Course_Section course_section : query) {
                List<Course_Lesson> lessonBySectionId = findLessonBySectionId(course_section.getId());
                course_section.setList(lessonBySectionId);
            }
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }

    }

    @Override
    public List<Course_Lesson> findLessonBySectionId(Integer id) {
        try {

            String sql = "select id,course_id,section_id,theme,duration,is_free,order_num,`status`\n" +
                    "from course_lesson where section_id = ?  ";
            List<Course_Lesson> query = qr.query(sql, new BeanListHandler<Course_Lesson>(Course_Lesson.class), id);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    @Override
    public Course findCourseByCourseId(Integer courseId) {
        try {

            String sql = "select id,course_name from course where id = ?" ;
            Course query = qr.query(sql, new BeanHandler<Course>(Course.class), courseId);
            return query;
        } catch (SQLException throwables) {

            throwables.printStackTrace();
            return null;
        }
    }

    @Override
    public int saveSection(Course_Section courseSection) {
        try {
            String sql = "INSERT INTO course_section ( course_id, section_name, description, order_num, STATUS, create_time, update_time )\n" +
                    "VALUES\n" +
                    "\t( ?,?,?,?,?,?,? )";
            Object [] params = {courseSection.getCourse_id(),courseSection.getSection_name(),courseSection.getDescription(),courseSection.getOrder_num(),courseSection.getStatus()
            ,courseSection.getCreate_time(),courseSection.getUpdate_time()};
            int update = qr.update(sql, params);
            return update;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }

    }
    // 修改章节
    @Override
    public int updateSection(Course_Section courseSection) {
        try {
           /* QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());*/
            String sql = "update course_section set section_name=?,description=?,order_num=?,update_time=? where id=?";
            Object [] params = {courseSection.getSection_name(),courseSection.getDescription(),courseSection.getOrder_num(),courseSection.getUpdate_time(),courseSection.getId()};
            int update = qr.update(sql, params);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }

    }

    @Override
    public int updateLesson(Course_Lesson courseLesson) {
        try {
            String sql = "update course_lesson set course_id =?,section_id=?,theme=?,duration=?,is_free=?,order_num=?, update_time=? where id=?";
            Object [] params = {courseLesson.getCourse_id(),courseLesson.getSection_id(),courseLesson.getTheme(),courseLesson.getDuration(),courseLesson.getIs_free(),courseLesson.getOrderNum(),courseLesson.getUpdate_time(),courseLesson.getId()};
            int result = qr.update(sql,params);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }

    }

    @Override
    public int insertLesson(Course_Lesson courseLesson) {
        try {
            String sql = " INSERT INTO course_lesson ( course_id, section_id, theme, duration, is_free, order_num,create_time, update_time )\n" +
                    "VALUES\n" +
                    "\t( ?,?,?,?,?,?,?,? )";
            Object [] params = {courseLesson.getCourse_id(),courseLesson.getSection_id(),courseLesson.getTheme(),courseLesson.getDuration(),courseLesson.getIs_free(),courseLesson.getOrderNum(),courseLesson.getCreate_time(),courseLesson.getUpdate_time()};
            int update = qr.update(sql, params);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }
    }
}
